/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.alxbrendmaquinarias.service;

import com.alxbrendmaquinarias.model.Product;
import com.alxbrendmaquinarias.model.ProductShop;
import com.alxbrendmaquinarias.model.Shop;
import com.alxbrendmaquinarias.repository.GenericDAO;
import com.alxbrendmaquinarias.util.VarUtil;
import com.alxbrendmaquinarias.util.faces.FacesUtil;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Max
 */
@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    GenericDAO genericDAO;

    @Override
    public List list(String namedQuery) throws Exception {
        return genericDAO.findWithNamedQuery(namedQuery);
    }

    @Override
    public void save(Shop shop) throws Exception {
        //TRAER LOS PRECIOS DE OTRA TIENDA. SI ES QUE EXISTE
        Shop shopTemp = genericDAO.create(shop);
        List<Product> list = genericDAO.findWithNamedQuery("Product.findAll");
        for (Product list1 : list) {
            ProductShop ps = new ProductShop();
            ps.setAmount(0);
            ps.setOutputPrice(0.0);
            ps.setShopId(shopTemp);
            ps.setProductId(list1);
            ps.setTypeMoney(VarUtil.MONEDA_SOLES);
            genericDAO.create(ps);
        }
        FacesUtil.addMessage("Actualización", "Se ha añadido " + list.size() + " productos a la tienda", FacesMessage.SEVERITY_INFO);
    }

    @Override
    public void update(Shop shop) throws Exception {
        genericDAO.update(shop);
    }

    @Override
    public void remove(Shop shop) throws Exception {
        genericDAO.delete(shop);
    }

    @Override
    public List list(String namedQuery, Map<String, Object> parameters) throws Exception {
        return genericDAO.findWithNamedQuery(namedQuery, parameters);
    }

    @Override
    public Shop FindShop(Integer id) {
        return genericDAO.find(Shop.class, id);
    }

}
